package com.qf.coupon2203.getcoupon.consume;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.coupon2203.domain.constants.Constants;
import com.qf.coupon2203.domain.entity.Coupon;
import com.qf.coupon2203.getcoupon.esvo.CouponInfo;
import com.qf.coupon2203.getcoupon.repository.CouponInfoResitory;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;


@Component
public class MyConsume {

    @Autowired
    CouponInfoResitory couponInfoResitory;


    @RabbitListener(queues = Constants.UPDATEQUEUE)
    public void saveCouponToRedis(String msg, Channel channel, Message message) throws IOException {

        String[] split = msg.split(":");
        String name = split[0];
        String phone = split[1];
        String couponId = split[2];

        CouponInfo couponInfo = new CouponInfo();

        couponInfo.setName(name);
        couponInfo.setPhone(phone);
        couponInfo.setCouponId(couponId);

        couponInfoResitory.save(couponInfo);


        //手工ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
